<?php


/*
 * Displays articles for the Front Page, a Category or individual Articles.
 */
class Blackbird_ContentController extends Zend_Controller_Action
{
	function indexAction()
	{
		$params = array(
			'mainArticlesCount' => (int)$this->_getParam('mainArticlesCount', 1),
			'summaryArticlesCount' => (int)$this->_getParam('summaryArticlesCount', 10),
			'linkArticlesCount' => (int)$this->_getParam('linkArticlesCount', 4),
		);
		$pageIndex = (int)$this->_getParam('pageIndex');

		$model = new Blackbird_Model_Content();
		$flashMessenger = $this->_helper->getHelper('FlashMessenger');
		$this->view->messages = $flashMessenger->getMessages();
		$this->view->articles = $model->fetchFrontPageArticles($params, $pageIndex);
		$this->view->params = $params;
	}

	function categoryAction()
	{
		$params = array(
			'mainArticlesCount' => (int)$this->_getParam('mainArticlesCount', 1),
			'summaryArticlesCount' => (int)$this->_getParam('summaryArticlesCount', 10),
			'linkArticlesCount' => (int)$this->_getParam('linkArticlesCount', 4),
		);
		$pageIndex = (int)$this->_getParam('pageIndex');
		$slug = $this->_getParam('slug');

		$model = new Blackbird_Model_Content();
		$category = $model->fetchCategoryBySlug($slug);
		if (false === $category)
		{
			$flashMessenger = $this->_helper->getHelper('FlashMessenger');
			$flashMessenger->addMessage('Category not found.');
			$this->_redirect(BASE_URL . '/', array('code' => 303));
		}
		$this->view->category = $category;
		$this->view->articles = $model->fetchArticlesByCategoryId($params, $pageIndex, $category->categoryId);
		$this->view->children = $model->fetchChildrenByCategoryId($category->categoryId);
		$this->view->params = $params;
	}

	function articleAction()
	{
		$slug = $this->_getParam('slug');
		$model = new Blackbird_Model_Content();
		$article = $model->fetchArticleBySlug($slug);
		if (false === $article)
		{
			$flashMessenger = $this->_helper->getHelper('FlashMessenger');
			$flashMessenger->addMessage('Article not found.');
			$this->_redirect(BASE_URL . '/', array('code' => 303));
		}
		$request = $this->getRequest();
		$commentForm = new Blackbird_Form_Content_Comment();
		$commentForm
			->addElement('hidden', 'articleId')
			->setDefaults(array('articleId' => $article->articleId));
		$commentForm->articleId
			->removeDecorator('DtDdWrapper')
			->removeDecorator('Label');
		if ($request->isPost())
		{
			if ($commentForm->isValid($request->getPost()))
			{
				if ($model->postArticleComment($commentForm) !== false)
				{
					$this->_helper->getHelper('FlashMessenger')
						->addMessage('Comentario publicado.');
					$this->_redirect($request->getRequestUri(), array('code' => 303));
					return;
				}
			}
		}
		$this->view->article = $article;
		$this->view->articleComments = $model->fetchArticleCommentsFromArticleId($article->articleId);
		$this->view->commentForm = $commentForm;
	}

	function shareAction()
	{
		$slug = $this->_getParam('articleSlug');
		$model = new Blackbird_Model_Content();
		$article = $model->fetchArticleBySlug($slug);
		if (false === $article)
		{
			$this->_helper->getHelper('FlashMessenger')
				->addMessage('No existe ningún artículo "' . $slug . '".');
			return $this->_redirect(BASE_URL, array('code' => 303));
		}
		$form = new YoAndo_Form_Events_Share();
		$request = $this->getRequest();
		if ($request->isPost() && $form->isValid($request->getPost()))
		{
			$model->share($form, $article);
			$this->_helper->getHelper('FlashMessenger')
				->addMessage('El artículo ha sido enviado a su amigo.');
			return $this->_redirect(BASE_URL . '/articles/' . $article->slug, array('code' => 303));
		}
		$form->setDefaults(array(
			'slug' => $article->slug,
		));
		$this->event = $article;
		$this->view->form = $form;
	}

	function linkAction()
	{
		$address = $this->_getParam('address');
		return $this->_redirect($address, array('code' => 303));
	}

	function routeAction()
	{
		$model = new Blackbird_Model_Content();
		$slugs = $this->_getParam('slugs');
		$last = $slugs[count($slugs) - 1];
		if ('share' == $last)
			return $this->_forward('share');
		$this->_setParam('slug', $last);	// passing last slug.
		$action = 'index';
		foreach (array_reverse($slugs) as $slug)
		{
			$action = 'category';
			if ($model->existsArticleBySlug($slug))
			{
				$action = 'article';
				break;
			}
		}
		$this->_forward($action);
	}

	function blocksAction()
	{
		$position = $this->_getParam('position');
		$section  = $this->_getParam('section');

		$model = new Blackbird_Model_Content();
		$blocks = $model->fetchBlocksByPosition($position, $section);

		// Clone the view object to prevent over-writing of view variables
		$viewRendererObj = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		Zend_Controller_Action_HelperBroker::addHelper(clone $viewRendererObj); 
		$dispatcher = clone Zend_Controller_Front::getInstance()->getDispatcher(); 
		$directories = array();
		foreach ($dispatcher->getControllerDirectory() as $module => $directory)
			$directories[$module] = str_replace('controllers', 'blocks', $directory);
		$dispatcher->setControllerDirectory($directories);
		foreach ($blocks as $block)
		{
			$request  = clone $this->getRequest();
			$response = clone $this->getResponse();

			$request->setParams($block->params)
				->setModuleName($block->module)
				->setControllerName($block->controller)
				->setActionName($block->action)
				->setDispatched(true)
				->setParam('block', $block);
			$dispatcher->dispatch($request, $response); 

			$block->response = ($request->isDispatched() && !$response->isRedirect())
				? $response->getBody()
				: '';
		}
		Zend_Controller_Action_HelperBroker::addHelper($viewRendererObj);

		$this->view->blocks = $blocks;
		$this->view->position = $position;
	}
}

